package com.example.bookshop.service.user;

import com.example.bookshop.entity.Business;
import com.example.bookshop.entity.Customer;
import com.example.bookshop.repository.BusinessRepository;
import com.example.bookshop.repository.CustomerRepository;
import com.example.bookshop.result.Result;
import com.example.bookshop.util.MyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.naming.spi.DirStateFactory;
import javax.servlet.http.HttpSession;

@Service
public class CustomerServiceImpl implements CustomerService{
    @Autowired
    private CustomerRepository customerRepository;
    @Autowired
    private BusinessRepository businessRepository;

    @Override
    public int addCustomer(Customer customer) {
        if(customerRepository.findCustomerByTel(customer)==null){
            int i=customerRepository.addCustomer(customer);
            Business business=new Business();
            business.setCustomerId(customerRepository.findCustomerByTel(customer).getId());
            int j=businessRepository.addBusiness(business);
            return i;

        }
        else return 0;
    }

    @Override
    public Result Login(Customer customer,HttpSession session) {
        Customer customer1=customerRepository.findCustomerByTel(customer);
        if(customer1!=null){
            session.setAttribute("customer",customer1);
            return new Result(200);
        }
        else return new Result(400);
    }

    @Override
    public Customer getCustomer(HttpSession session) {
        Customer customer = (Customer) session.getAttribute("customer");
        return customer;

    }
}
